package cn.admin.remoting.util;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class NamedThreadFactory implements ThreadFactory {

    private final AtomicInteger threadNumber;
    private final String namePrefix;
    private final boolean isDaemon;

    public NamedThreadFactory(String name) {
        this(name,false);
    }

    public NamedThreadFactory(String prefix,boolean isDaemon) {
        this.threadNumber = new AtomicInteger(1);
        this.isDaemon = isDaemon;
        this.namePrefix = prefix + "-thread-";
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r,this.namePrefix + this.threadNumber.getAndIncrement());
        t.setDaemon(this.isDaemon);
        if (t.getPriority() != 5) {
            t.setPriority(5);
        }
        return t;
    }
}
